package org.jeecg.talentCooperation.thread;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.text.similarity.LevenshteinDistance;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.jeecg.talentCooperation.fieldKeyword.entity.FieldKeywordPo;
import org.jeecg.talentCooperation.fieldKeyword.service.IFieldKeywordService;
import org.jeecg.talentCooperation.fruit.entity.*;
import org.jeecg.talentCooperation.fruit.mapper.FruitAuthorMapper;
import org.jeecg.talentCooperation.fruit.mapper.FruitMapper;
import org.jeecg.talentCooperation.fruit.model.FruitResultModel;
import org.jeecg.talentCooperation.fruit.service.*;
import org.jeecg.talentCooperation.sysdict.po.NdSysDict;
import org.jeecg.talentCooperation.sysdict.service.INdSysDictService;
import org.jeecg.talentCooperation.talent.entity.TalentBasic;
import org.jeecg.talentCooperation.talent.entity.TalentBasicDealInfo;
import org.jeecg.talentCooperation.talent.entity.TalentFieldRel;
import org.jeecg.talentCooperation.talent.mapper.TalentBasicMapper;
import org.jeecg.talentCooperation.talent.service.ITalentBasicService;
import org.jeecg.talentCooperation.talent.service.ITalentFieldRelService;
import org.jeecg.talentCooperation.util.CommonUtil;
import org.jeecg.talentCooperation.util.DateConverterUtil;
import org.jeecg.talentCooperation.util.PinyinUtil;
import org.jeecg.talentCooperation.util.SnowflakeIdUtil;
import org.jeecg.talentCooperation.wordcloud.entity.WordcloudPo;
import org.jeecg.talentCooperation.wordcloud.service.IWordSynonymsService;
import org.jeecg.talentCooperation.wordcloud.service.IWordcloudService;
import org.opensaml.xml.signature.Q;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.*;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
@Slf4j
public class AsyncService {

    @Autowired
    IFruitService fruitService;
    @Autowired
    private IFruitAuthorService fruitAuthorService;
    @Autowired
    private IFruitAuthorOrgService fruitAuthorOrgService;
    @Autowired
    private IFruitFieldRelService fruitFieldRelService;
    @Autowired
    private IFruitFieldTagRelService fruitFieldTagRelService;
    @Autowired
    private IFruitTalentFieldRelService fruitTalentFieldRelService;
    @Autowired
    private ITalentFieldRelService talentFieldRelService;
    @Autowired
    private INdSysDictService sysDictService;
    @Autowired
    private ITalentBasicService basicService;
    @Autowired
    IWordcloudService wordcloudService;
    @Autowired
    IFieldKeywordService fieldKeywordService;
    @Autowired
    IWordSynonymsService wordSynonymsService;
    @Autowired
    private FruitMapper fruitMapper;
    private FruitAuthorMapper fruitAuthorMapper;
    @Autowired
    private TalentBasicMapper talentBasicMapper;


    /**
     * 这里进行标注为异步任务，在执行此方法的时候，会单独开启线程来执行
     */
    @Transactional(rollbackFor = Exception.class)
//    @Async("myExecutor")
    public void dealFruit(List<JSONObject> jsonObjectList, Map<String, String> sysDictMap, boolean expand,
                          List<NdSysDict> toUpdateNdSysDictList, List<FruitFieldRel> toUpdateFruitFieldRelList,
                          List<TalentBasic> talentBasicList, Map<String, TalentBasic> talentBasicMap, List<TalentBasic> toUpdateBasicList,
                          Map<String, Fruit> fruitListMap, List<Fruit> toUpdateFruitList,
                          List<FruitAuthor> fruitAuthorList, List<FruitAuthorOrg> fruitAuthorOrgList) throws Exception {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        SnowflakeIdUtil idWorker = new SnowflakeIdUtil(1, 1);
        int index = 0;
        for (JSONObject jsonObject : jsonObjectList) {
            String title = jsonObject.getString("title");
            if (StringUtils.isEmpty(title)) {
                continue;
            }

            if(!ObjectUtils.isEmpty(fruitListMap.get(title))){
                continue;
            }
            System.out.println("----正在读取成果第----" + index++ + "----条----");
            Fruit fruit = new Fruit();
            long fruitId = idWorker.nextId();

            String url = jsonObject.getString("url");
            String dbcode = jsonObject.getString("dbcode");
            String dbcode_origin = jsonObject.getString("dbcode_origin");
            String dbcode_name = jsonObject.getString("dbcode_name");
            String abstractContent = jsonObject.getString("abstract");
            String date = DateConverterUtil.convertToDateString(jsonObject.getString("date"));
            String researchDomain = "";
            JSONArray orgNames = null;
            String authors = null;
            String patentAuthors = "";

            fruit.setId(fruitId);
            fruit.setName(title);
            fruit.setLinkUrl(url);
            fruit.setSummary(abstractContent);
            fruit.setPubTime(!ObjectUtils.isEmpty(date) ? sdf.parse(date) : null);

            //专利  1=期刊。3=学位论文；5=会议；7=专利
            if (("7").equals(dbcode)) {
                fruit.setType("11");
                JSONArray brief = jsonObject.getJSONArray("brief");
                String[] briefTemp = new String[brief.size()];
                String[] briefs = brief.toArray(briefTemp);

                for (String s : briefs) {
                    String value = s.split("：")[1];
                    if (s.contains("专利类型")) {
                        fruit.setPatentType(value);
                    } else if (s.contains("申请(专利)号")) {
                        fruit.setReadNumber(value);
                    } else if (s.contains("申请日")) {
                        fruit.setPubTime(sdf.parse(value));
                    } else if (s.contains("申请公布号")) {
                        fruit.setIssueNumber(value);
                    } else if (s.contains("申请公布日")) {
                        fruit.setCompletionTime(sdf.parse(value));
                    } else if (s.contains("多次公布")) {
                        fruit.setPubCycle(value);
                    } else if (s.contains("申请人")) {
                        fruit.setOrganizer(value);
                    } else if (s.contains("地址")) {
                        fruit.setLinkAddress(value);
                    } else if (s.contains("发明人")) {
                        patentAuthors = value;
                    } else if (s.contains("专辑")) {
                        fruit.setTheme(value);
                    } else if (s.contains("专题")) {
                        researchDomain = value;
                    } else if (s.contains("主分类号")) {
                        fruit.setSerialNumber(value);
                    } else if (s.contains("分类号")) {
                        fruit.setCirculation(value);
                    } else if (s.contains("国省代码")) {
                        fruit.setLanguages(value);
                    } else if (s.contains("页数")) {
                        fruit.setJournalName(value);
                    } else if (s.contains("代理机构")) {
                        fruit.setUndertaker(value);
                    } else if (s.contains("代理人")) {
                        fruit.setCooperator(value);
                    }
                }

            } else {
                if (("1").equals(dbcode)) {
                    fruit.setType("6");
                    fruit.setPaperType("1");
                    fruit.setPubTime(!ObjectUtils.isEmpty(date) ? sdf.parse(date) : null);
                } else if (("3").equals(dbcode)) {
                    fruit.setType("6");
                    fruit.setPaperType("3");
                    fruit.setPubTime(!ObjectUtils.isEmpty(date) ? sdf.parse(date) : null);
                } else if (("5").equals(dbcode)) {
                    fruit.setType("4");
                    fruit.setActivityProdType("1");
                    fruit.setPaperType("2");

                    JSONObject brief = jsonObject.getJSONObject("brief");
                    if (brief != null) {
                        fruit.setTheme(brief.getString("name"));
                        String location = brief.getString("location");
//                        String time = brief.getString("time");
//                        if (!StringUtils.isEmpty(time)) {
//                            if (CommonUtil.isDateValid(time, "yyyy-MM-dd") || CommonUtil.isDateValid(time, "yyyy-MM")) {
//                                fruit.setPubTime(sdf.parse(time.length() < 10 ? (time + "-01") : time));
//                            } else {
//                                fruit.setPubPlace(time);
//                            }
//                        }
                        if (!StringUtils.isEmpty(location)) {
                            if (CommonUtil.isDateValid(location, "yyyy-MM-dd") || CommonUtil.isDateValid(location, "yyyy-MM")) {
                                fruit.setPubTime(sdf.parse(location.length() < 10 ? (location + "-01") : location));
                            } else {
                                fruit.setPubPlace(location);
                            }
                        }
                    }
                }

                String keywords = jsonObject.getString("keywords");
                authors = jsonObject.getString("authors");

                JSONArray departments = jsonObject.getJSONArray("departments");
                if (departments != null) {
//                    String[] deptstrs = new String[departments.size()];
                    // 使用JSONArray 中的toArray进行转换
                    orgNames = departments;
                }

                JSONArray topSpace = jsonObject.getJSONArray("topSpace");
                if (topSpace != null) {
                    String[] topstrs = new String[topSpace.size()];
                    //使用JSONArray 中的toArray进行转换
                    String[] topSpaceArr = topSpace.toArray(topstrs);
                    for (String s : topSpaceArr) {
                        if (s.contains("专题")) {
                            researchDomain = s.split("：")[1];
                            break;
                        }
                    }
                }

                fruit.setKeyword(keywords);
//            fruit.setIssueNumber(topTip);
            }
            fruit.setCreateTime(new Date());
            toUpdateFruitList.add(fruit);

            fruitListMap.put(fruit.getName(), fruit);

            //researchDomain 领域处理
//            setFruitField(sysDictMap, toUpdateNdSysDictList, toUpdateFruitFieldRelList, researchDomain, fruitId);

            //专利
            if (("7").equals(dbcode)) {
                String[] patentAuthorArr = patentAuthors.split(";");
                for (int i = 0; i < patentAuthorArr.length; i++) {
                    String authorName = patentAuthorArr[i];

                    long authorId = idWorker.nextId();
                    FruitAuthor fruitAuthor = addAuthor(expand ,authorId, authorName, talentBasicList, talentBasicMap, toUpdateBasicList, researchDomain, orgNames, "", fruitId, i + 1);
                    fruitAuthorList.add(fruitAuthor);
                }
            } else {
                this.dealAuthorAndOrgName(expand ,authors, idWorker, fruitId, researchDomain, talentBasicList, talentBasicMap, toUpdateBasicList, fruitAuthorList, fruitAuthorOrgList, orgNames);
            }
        }

    }

    public void setFruitField(Map<String, String> sysDictMap, List<NdSysDict> toUpdateNdSysDictList,
                              List<FruitFieldRel> toUpdateFruitFieldRelList, String researchDomain, long fruitId) {
        // 拆分研究领域并保存
        String[] domains = researchDomain.split("[\\p{Punct}]+");
        for (String domain : domains) {
            if (!StringUtils.isEmpty(domain)) {
                String value = determineValue(sysDictMap, toUpdateNdSysDictList, domain);
//                FruitField fruitField = new FruitField(fruitId, value, "", false, new Date());
//                toUpdateFruitFieldList.add(fruitField);
            }
        }
    }

    private String determineValue(Map<String, String> sysDictMap, List<NdSysDict> toUpdateNdSysDictList, String domain) {
        String value;
        if (!sysDictMap.containsKey(domain)) {
            value = (String.valueOf(sysDictMap.size() + 1));
            sysDictMap.put(domain, value);

            NdSysDict ndSysDict = new NdSysDict();
            ndSysDict.setDComment(domain);
            ndSysDict.setDName(domain);
            ndSysDict.setDValue(value);
            ndSysDict.setTypeKey("ResearchField");
            ndSysDict.setIsDel(false);
            ndSysDict.setParentId(113071605385521293L);
            ndSysDict.setCreateTime(new Date());
            ndSysDict.setUpdateTime(new Date());

            toUpdateNdSysDictList.add(ndSysDict);
        } else {
            value = sysDictMap.get(domain);
        }
        return value;
    }

    public void dealAuthorAndOrgName(boolean expand, String authors, SnowflakeIdUtil idWorker, long fruitId, String researchDomain, List<TalentBasic> talentBasicList, Map<String, TalentBasic> talentBasicMap, List<TalentBasic> toUpdateBasicList, List<FruitAuthor> fruitAuthorList, List<FruitAuthorOrg> fruitAuthorOrgList, JSONArray orgNames) {

        // 判断并解析 JSON 对象
        if (!ObjectUtils.isEmpty(authors) && authors.startsWith("{") && authors.endsWith("}")) {
            JSONObject jsonObject = JSON.parseObject(authors);
            int index = 1;
            // 遍历authors对象
            for (String key : jsonObject.keySet()) {
                // 获取每个作者的departments数组
                JSONArray departments = jsonObject.getJSONObject(key).getJSONArray("departments");
                String email = jsonObject.getJSONObject(key).getString("email");
//                String[] deptstrs = new String[departments.size()];
//                if(ObjectUtils.isEmpty(deptstrs)){
//                    System.out.println("------部门还能有空的-----" + departments.toJSONString());
//                }

                //新增作者
                long authorId = idWorker.nextId();
                setAuthorInfo(expand, authorId, key, talentBasicList, talentBasicMap, toUpdateBasicList, fruitAuthorList, departments, email, researchDomain, fruitId, index);

                //新增机构
                setAuthorOrgInfo(idWorker, authorId, fruitAuthorOrgList, departments, 1);

                index++;
            }
        }
    }

    public void setAuthorInfo(boolean expand, long authorId, String authorName, List<TalentBasic> talentBasicList, Map<String, TalentBasic> talentBasicMap, List<TalentBasic> toUpdateBasicList,
                              List<FruitAuthor> fruitAuthorList, JSONArray depts,  String email, String researchDomain, long fruitId, int authorRank) {
        FruitAuthor fruitAuthor = addAuthor(expand, authorId, authorName, talentBasicList, talentBasicMap, toUpdateBasicList, researchDomain, depts, email, fruitId, authorRank);
        fruitAuthorList.add(fruitAuthor);
    }

    public FruitAuthor addAuthor(boolean expand, long authorId, String authorName, List<TalentBasic> talentBasicList, Map<String, TalentBasic> talentBasicMap, List<TalentBasic> toUpdateBasicList,
                                 String researchDomain, JSONArray depts, String email, Long fruitId, int rank) {

        TalentBasic talentBasic = getTalentBasic(expand, authorId, authorName, talentBasicList,  talentBasicMap, toUpdateBasicList, depts, email, researchDomain);

        FruitAuthor fruitAuthor = new FruitAuthor();
        fruitAuthor.setId(authorId);
        fruitAuthor.setAuthor(authorName);
        fruitAuthor.setOrgUserId(Long.parseLong(talentBasic.getId()));
        fruitAuthor.setCreateTime(new Date());
        fruitAuthor.setFruitId(fruitId);
        fruitAuthor.setIsDel(false);
        fruitAuthor.setRanking(rank);
        return fruitAuthor;
    }

//    public void dealAuthorAndOrgName(JSONArray authors, SnowflakeIdUtil idWorker, long fruitId, String researchDomain, List<FruitAuthor> fruitAuthorList, List<FruitAuthorOrg> fruitAuthorOrgList, String[] orgNames){
//
//        QueryWrapper queryAuthorWrapper = new QueryWrapper();
//        queryAuthorWrapper.eq("fruit_id", fruitId);
//        List<FruitAuthor> authorList = fruitAuthorService.list(queryAuthorWrapper);
//        //已生成的作者id
//        List<Long> existAuthorIdList = authorList.stream().map(FruitAuthor::getId).collect(Collectors.toList());

//        for (int i = 0; i < authors.size(); i++) {
//            String author = authors.get(i).toString();

    //判断最后一位是不是数字
//            String authorLastIndexStr = author.substring(author.length()-1);
//            if(CommonUtil.isContainNumber(authorLastIndexStr)){
//                if(author.contains(",")){
//                    //----------------------------------------------------------------------------------------------------------------
//                    //"authors": ["李先军1", "黄速建1,2"], "departments": ["1. 中国社会科学院工业经济研究所", "2. 中国企业管理研究会"]
//                    //----------------------------------------------------------------------------------------------------------------
//                    String[] authorNameArr = author.split(",");
//                    String firstAuthorName = authorNameArr[0];
//                    //判断拆分后的作者名称 第一个作者，最后一位是否包含数字，包含，则是上面这种格式的作者
//                    if(CommonUtil.isContainNumber(firstAuthorName.substring(firstAuthorName.length()-1))){
//                        //获取作者姓名
//                        String realAuthorName = firstAuthorName.substring(0, firstAuthorName.length()-1);
//                        //新增作者
//                        long authorId = idWorker.nextId();
//                        setAuthorInfo(authorId, realAuthorName, fruitAuthorList, researchDomain, fruitId, i+1);
//
//                        List<String> orgNameIndexList = new ArrayList<>();
//                        orgNameIndexList.add(firstAuthorName.substring(firstAuthorName.length()-1));
//                        //获取作者单位--去除第一个, 从1开始
//                        orgNameIndexList.addAll(Arrays.asList(authorNameArr).subList(1, authorNameArr.length));
//
//                        for (String orgNameIndex : orgNameIndexList) {
//                            setAuthorOrgInfo(idWorker, authorId, existAuthorIdList, fruitAuthorOrgList, orgNames, Integer.parseInt(orgNameIndex));
//                        }
//                    }
//                }else{
//                    //----------------------------------------------------------------------------------------------------------------------
//                    //"authors": ["张凯杰1", "丁国锋1", "闻铭1", "惠红勋2"], "departments": ["1. 河海大学能源与电气学院", "2. 浙江大学电气工程学院"]
//                    //----------------------------------------------------------------------------------------------------------------------
//                    String authorName = author.substring(0, author.length()-1);
//                    long authorId = idWorker.nextId();
//                    setAuthorInfo(authorId, authorName, fruitAuthorList, researchDomain, fruitId, i+1);
//
//                    setAuthorOrgInfo(idWorker, authorId, existAuthorIdList, fruitAuthorOrgList, orgNames, Integer.parseInt(authorLastIndexStr));
//                }
//            }else{
//                if(author.contains(",")){
//                    //----------------------------------------------------------------------------------------------------------------------
//                    //"authors": ["曹海欧,张量,陈久林,袁宇波,刘一丹"], "departments": ["江苏省电力试验研究院有限公司", "江苏省电力试验研究院有限公司"]
//                    //----------------------------------------------------------------------------------------------------------------------
//                    String[] authorArr = author.split(",");
//                    for (int m = 0; m < authorArr.length; m++) {
//                        long authorId = idWorker.nextId();
//                        setAuthorInfo(authorId, authorArr[m], fruitAuthorList, researchDomain, fruitId, m+1);
//
//                        for (int j = 0; j < orgNames.length; j++) {
//                            setAuthorOrgInfo(idWorker, authorId, existAuthorIdList, fruitAuthorOrgList, orgNames[j], j+1);
//                        }
//                    }
//                }else{
//                    //----------------------------------------------------------------------------------------------------------------------
//                    //"authors": ["尚楠", "张翔", "涂腾", "丁一"], "departments": ["南方电网能源发展研究院有限责任公司", "浙江大学电气工程学院"]
//                    //"authors": ["“21世纪中国管理学发展研究”课题组", "黄速建", "黄群慧", "张蒽", "钟宏武", "焦淑艳", "朱昊伟"], "departments": ["中国社会科学院工业经济研究所 北京100836"]}
//                    //----------------------------------------------------------------------------------------------------------------------
//                    long authorId = idWorker.nextId();
//                    setAuthorInfo(authorId, author, fruitAuthorList, researchDomain, fruitId, i+1);
//
//                    for (int j = 0; j < orgNames.length; j++) {
//                        setAuthorOrgInfo(idWorker, authorId, existAuthorIdList, fruitAuthorOrgList, orgNames[j], j+1);
//                    }
//                }
//            }
//        }
//    }

    @Transactional
    public TalentBasic getTalentBasic(boolean expand, long authorId, String authorName, List<TalentBasic> talentBasicList, Map<String, TalentBasic> talentBasicMap, List<TalentBasic> toUpdateBasicList, JSONArray depts, String email, String researchDomain) {
        // 获取到的值
        String newKey = authorName.toLowerCase() + "##" + (!ObjectUtils.isEmpty(depts) && !ObjectUtils.isEmpty(depts.get(0)) ? depts.get(0) : "");
        // 过滤包含此作者list
        List<TalentBasic> filteredTalentBasicList = talentBasicList.stream().filter(talentBasic -> talentBasic.getRealName().toLowerCase().equals(authorName.toLowerCase())).collect(Collectors.toList());
        TalentBasic talentBasic = null;
        for (TalentBasic filteredTalentBasic : filteredTalentBasicList) {
            //遍历相似度
            if(cosineSimilarity(filteredTalentBasic.getRealName().toLowerCase()+"##"+filteredTalentBasic.getCompany(), newKey)){
                talentBasic = filteredTalentBasic;
                break;
            }
        }

//        TalentBasic talentBasic = talentBasicMap.get(newKey);

        if( talentBasic == null && expand){
            talentBasic = new TalentBasic();
            talentBasic.setId(authorId +"");
            talentBasic.setRealName(authorName);
            talentBasic.setNamePinyin(PinyinUtil.getPinYinPrefix(authorName, true));
            talentBasic.setCompany(!ObjectUtils.isEmpty(depts) && !ObjectUtils.isEmpty(depts.get(0)) ? depts.get(0).toString() : "");
//            talentBasic.setResearchDomain(researchDomain);
            talentBasic.setMail(email);
            talentBasic.setFacultySources("2");
            talentBasic.setCreateTime(new Date());
            toUpdateBasicList.add(talentBasic);

//            talentBasicList.add(talentBasic);
            talentBasicMap.put(newKey, talentBasic);
        }
        return talentBasic;
    }

    public void setAuthorOrgInfo(SnowflakeIdUtil idWorker, long authorId, List<FruitAuthorOrg> fruitAuthorOrgList, JSONArray orgNames, int orgNameIndex) {
        long authorOrgId = idWorker.nextId();
        FruitAuthorOrg fruitAuthorOrg = new FruitAuthorOrg();

        fruitAuthorOrg.setAuthorId(authorId);
        fruitAuthorOrg.setIsDel(false);
        fruitAuthorOrg.setName(!ObjectUtils.isEmpty(orgNames) && !ObjectUtils.isEmpty(orgNames.get(0)) ? orgNames.get(0).toString() : "");
        fruitAuthorOrg.setRanking(1);
        fruitAuthorOrg.setCreateTime(new Date());
        fruitAuthorOrg.setId(authorOrgId);

        fruitAuthorOrgList.add(fruitAuthorOrg);
    }

    public void dealData(List<NdSysDict> toUpdateNdSysDictList, List<FruitFieldRel> toUpdateFruitFieldRelList, List<TalentBasic> toUpdateBasicList,
                         List<Fruit> toUpdateFruitList, List<FruitAuthor> fruitAuthorList, List<FruitAuthorOrg> fruitAuthorOrgList){

        if (!CollectionUtils.isEmpty(toUpdateNdSysDictList)) {
            System.out.println("-------共读取到新字典表值-----" + toUpdateNdSysDictList.size() + "----个----");
            batchProcess(toUpdateNdSysDictList, sysDictService::saveOrUpdateBatch);
        }

        if (!CollectionUtils.isEmpty(toUpdateFruitFieldRelList)) {
            System.out.println("-------共读取到新成果对应领域关系-----" + toUpdateFruitFieldRelList.size() + "----个----");
            batchProcess(toUpdateFruitFieldRelList, fruitFieldRelService::saveOrUpdateBatch);
        }

        if (!CollectionUtils.isEmpty(toUpdateBasicList)) {
            System.out.println("-------共读取到新专家-----" + toUpdateBasicList.size() + "----个----");
            batchProcess(toUpdateBasicList, basicService::saveOrUpdateBatch);
        }

        if (!CollectionUtils.isEmpty(toUpdateFruitList)) {
            System.out.println("-------共读取到新成果-----" + toUpdateFruitList.size() + "----个----");
            batchProcess(toUpdateFruitList, fruitService::saveOrUpdateBatch);
        }
        if (!CollectionUtils.isEmpty(fruitAuthorList)) {
            System.out.println("-------共读取到新成果作者-----" + fruitAuthorList.size() + "----个----");
            batchProcess(fruitAuthorList, fruitAuthorService::saveOrUpdateBatch);
        }
        if (!CollectionUtils.isEmpty(fruitAuthorOrgList)) {
            System.out.println("-------共读取到新成果作者机构-----" + fruitAuthorOrgList.size() + "----个----");
            batchProcess(fruitAuthorOrgList, fruitAuthorOrgService::saveOrUpdateBatch);
        }

    }

    public void dealExpertSimilarity(List<TalentBasic> talentBasicList) {
        Map<TalentBasic, List<TalentBasic>> resultMap = mergeSimilarStrings(talentBasicList);
        if(!CollectionUtils.isEmpty(resultMap)){
            for (Map.Entry<TalentBasic, List<TalentBasic>> entry : resultMap.entrySet()) {
                TalentBasic keyEntity = entry.getKey();
                List<TalentBasic> similarEntities = entry.getValue();
                System.out.println("Key Entity: " + keyEntity.getId()+ "----"+ keyEntity.getRealName()+ "----" +keyEntity.getCompany());
                for (TalentBasic similarEntity : similarEntities) {
                    System.out.println("  Similar Entity: " +  similarEntity.getId()+ "----"+ similarEntity.getRealName()+ "----" +similarEntity.getCompany());
                }
//                List<String> similarUserIdList = similarEntities.stream().map(TalentBasic::getId).collect(Collectors.toList());
//                //更新成果作者id
//                QueryWrapper fruitAuthorWrapper = new QueryWrapper();
//                fruitAuthorWrapper.in("org_user_id", similarUserIdList);
//
//                FruitAuthor fruitAuthor = new FruitAuthor();
//                fruitAuthor.setOrgUserId(Long.parseLong(keyEntity.getId()));
//                fruitAuthorMapper.update(fruitAuthor, fruitAuthorWrapper);
//
//                //删除专家 id
//                QueryWrapper deleteBasicWrapper = new QueryWrapper();
//                fruitAuthorWrapper.in("id", similarUserIdList);
//                talentBasicMapper.delete(deleteBasicWrapper);
            }
        }



    }

    public static Map<TalentBasic, List<TalentBasic>> mergeSimilarStrings(List<TalentBasic> talentBasicList) {
        // 按名字分组
        Map<String, List<TalentBasic>> groupedByName = talentBasicList.stream()
                .collect(Collectors.groupingBy(TalentBasic::getRealName));
        // 存储相似人才的map
        Map<TalentBasic, List<TalentBasic>> similarTalentsMap = new HashMap<>();
        int index = 0;
        // 遍历分组后的map
        for (Map.Entry<String, List<TalentBasic>> entry : groupedByName.entrySet()) {
            List<TalentBasic> talentsWithName = entry.getValue();
            if (talentsWithName.size() > 1) { // 如果有多于一个的人才具有相同的名字
                index++;
                System.out.println("----共有----"+groupedByName.size() + "---条数据，正在处理---"+index+"---条");
                List<String> processedTalents = new ArrayList<>();
                for (int i = 0; i < talentsWithName.size(); i++) {
                    TalentBasic currentTalent = talentsWithName.get(i);
                    if (processedTalents.contains(currentTalent.getId())) {
                        // 如果当前人才已经被处理过，则跳过
                        continue;
                    }
                    List<TalentBasic> similarTalents = new ArrayList<>();
                    for (int j = i + 1; j < talentsWithName.size(); j++) {
                        TalentBasic compareTalent = talentsWithName.get(j);
                        boolean isSimilar = compareTalent != currentTalent && jaccardSimilarity(currentTalent.getCompany(), compareTalent.getCompany()) > 0.5;
                        if (isSimilar) {
                            //哪个的 信息完整  就用哪个
                            if(!ObjectUtils.isEmpty(compareTalent.getAllPhoneNumber()) && ObjectUtils.isEmpty(currentTalent.getAllPhoneNumber())){
                                currentTalent = compareTalent;
                            }
                            if(!ObjectUtils.isEmpty(compareTalent.getTalentNumber()) && ObjectUtils.isEmpty(currentTalent.getTalentNumber())){
                                currentTalent = compareTalent;
                            }
                            similarTalents.add(compareTalent);
                            // 将相似的人才也标记为已处理，以避免重复添加到其他人才的相似集合中
                            processedTalents.add(compareTalent.getId());
                        }
                    }
                    if (!similarTalents.isEmpty()) {
                        similarTalentsMap.putIfAbsent(currentTalent, similarTalents);
                        // 将当前人才也标记为已处理
                        processedTalents.add(currentTalent.getId());
                    }
                }
            }
        }
        return similarTalentsMap;


//        Map<TalentBasic, List<TalentBasic>> mergedMap = new HashMap<>();
//        Set<TalentBasic> processedAsKey = new HashSet<>();
//        int index = 0;
//        for (TalentBasic talentBasic : talentBasicList) {
//            index++;
//            System.out.println("----共有----"+talentBasicList.size() + "---条数据，正在处理---"+index+"---条");
//            if (processedAsKey.contains(talentBasic)) {
//                continue;
//            }
//
//            List<TalentBasic> similarEntities = new ArrayList<>();
//            for (TalentBasic compareEntity : talentBasicList) {
//                if (!talentBasic.equals(compareEntity) &&
//                        !processedAsKey.contains(compareEntity) &&
//                        cosineSimilarity(talentBasic.getRealName() + talentBasic.getCompany(), compareEntity.getRealName() + talentBasic.getCompany()))
//                    similarEntities.add(compareEntity);
//            }
//
//            if (!similarEntities.isEmpty()) {
//                mergedMap.put(talentBasic, similarEntities);
//                processedAsKey.add(talentBasic);
//            }
//        }
//        return mergedMap;
    }


    // 定义一个通用的批量处理函数
    private static <T> void batchProcess(List<T> list, Consumer<List<T>> batchOperation) {
        int batchSize = 2000;
        for (int i = 0; i < list.size(); i += batchSize) {
            int end = Math.min(i + batchSize, list.size());
            List<T> batch = list.subList(i, end);
            batchOperation.accept(batch);
        }
    }


//    public void setAuthorOrgInfo(SnowflakeIdUtil idWorker, long authorId, List<Long> existAuthorIdList, List<FruitAuthorOrg> fruitAuthorOrgList, String orgName, int orgNameIndex) {
//        long authorOrgId = idWorker.nextId();
//        FruitAuthorOrg fruitAuthorOrg = addAuthorOrg(authorId, existAuthorIdList, orgName, orgNameIndex);
//        fruitAuthorOrg.setId(authorOrgId);
//        fruitAuthorOrgList.add(fruitAuthorOrg);
//    }

//    public FruitAuthorOrg addAuthorOrg(long authorId, List<Long> existAuthorIdList, String orgName, int orgNameRank) {
//
//        //删除再添加
//        if (!CollectionUtils.isEmpty(existAuthorIdList)) {
//            QueryWrapper queryWrapper = new QueryWrapper();
//            queryWrapper.in("author_id", existAuthorIdList);
//            fruitAuthorOrgService.remove(queryWrapper);
//        }
//
//        FruitAuthorOrg fruitAuthorOrg = new FruitAuthorOrg();
//
//        fruitAuthorOrg.setAuthorId(authorId);
//        fruitAuthorOrg.setIsDel(false);
//        fruitAuthorOrg.setName(orgName);
//        fruitAuthorOrg.setRanking(orgNameRank);
//        fruitAuthorOrg.setCreateTime(new Date());
//        return fruitAuthorOrg;
//    }

    /**
     * 这里进行标注为异步任务，在执行此方法的时候，会单独开启线程来执行
     */
//    @Transactional
//    @Async("myExecutor")
    public void dealFruitAndTalentRel(List<Fruit> fruitList) {
        try {
            //所有专家
            List<TalentBasic> talentBasicList = basicService.list();
            Map<String, TalentBasic> talentBasicMap = talentBasicList.stream().collect(Collectors.toMap(TalentBasic::getRealName, Function.identity()));
            //所有成果作者
            List<FruitAuthor> fruitAuthorList = fruitAuthorService.list();
            //成果-作者名称连接 map
            Map<Long, String> fruitAuthorNamesMap = fruitAuthorList.stream().collect(
                    Collectors.groupingBy(FruitAuthor::getFruitId, Collectors.mapping(FruitAuthor::getAuthor, Collectors.toList()))
            ).entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, entry -> String.join(",", entry.getValue())));
            //成果-作者数组 map
            Map<Long, List<FruitAuthor>> fruitAuthorListMap = fruitAuthorList.stream().collect(Collectors.groupingBy(FruitAuthor::getFruitId));
            //所有词云
            List<WordcloudPo> wordCloudPoList = wordcloudService.list();
            Map<Long, WordcloudPo> wordCloudMap = wordCloudPoList.stream().collect(Collectors.toMap(WordcloudPo::getFruitId, Function.identity()));
            //国网关键词list
            List<FieldKeywordPo> gwFkList = fieldKeywordService.list();

            List<FruitTalentFieldRel> fruitTalentFieldRelList = new ArrayList<>();

            fruitList.forEach(fruit -> {
                String keywords = fruit.getKeyword();
                List<String> resultList = JSON.parseArray(keywords, String.class);
                //成果词云对应的 国网领域关键词
                Set<String> fkSetList = new HashSet<>();
                //成果词云对应的 国网研究领域
                Set<String> gwResearchFieldSetList = new HashSet<>();
                //没有设置过关键词
                if (!CollectionUtils.isEmpty(resultList) && ObjectUtils.isEmpty(wordCloudMap.get(fruit.getId()))) {
                    for (String word : resultList) {
                        if(!ObjectUtils.isEmpty(word)){
                            WordcloudPo po = new WordcloudPo();
                            String newWord = word.replace("。", "");
                            po.setFruitId(fruit.getId());
                            po.setName(newWord);
                            po.setCreateTime(new Date());
                            po.setUpdateTime(new Date());
                            wordCloudPoList.add(po);

                            //根据成果关键词 匹配国网关键词
                            List<FieldKeywordPo> matchList = gwFkList.stream().filter(fieldKeywordPo -> fieldKeywordPo.getName().contains(newWord)).collect(Collectors.toList());
                            fkSetList.addAll(matchList.stream().map(FieldKeywordPo::getName).collect(Collectors.toSet()));
                            gwResearchFieldSetList.addAll(matchList.stream().map(FieldKeywordPo::getResearchField).collect(Collectors.toSet()));
                        }
                    }
                }
                //每个成果对应的专家 作为单独的一条数据
                List<FruitAuthor> authorList = fruitAuthorListMap.get(fruit.getId());
                if(!ObjectUtils.isEmpty(authorList)){
                    authorList.forEach(fruitAuthor -> {
                        TalentBasic talentBasic = talentBasicMap.get(fruitAuthor.getAuthor());
                        if(talentBasic != null){
                            FruitTalentFieldRel fruitTalentFieldRel = new FruitTalentFieldRel(Long.parseLong(talentBasic.getId()) , talentBasic.getRealName(), talentBasic.getAllPhoneNumber(), talentBasic.getPartPhoneNumber(), talentBasic.getCooperationNum(), talentBasic.getCompany(),
                                    fruit.getId(), fruit.getName(), fruit.getKeyword(), fruitAuthorNamesMap.get(fruit.getId()), fruit.getType(), fruit.getPubTime(), fruit.getSummary(),
                                    String.join(",", fkSetList), String.join(",", gwResearchFieldSetList));
                            fruitTalentFieldRelList.add(fruitTalentFieldRel);
                        }
                    });
                }
            });

//            System.out.println("-------共匹配到国网关键词-----" + fruitTalentFieldRelList.size() + "----个----");
//            batchProcess(fruitTalentFieldRelList, fruitTalentFieldRelService::saveOrUpdateBatch);
//
//            System.out.println("-------共读取到关键词-----" + wordCloudPoList.size() + "----个----");
//            batchProcess(wordCloudPoList, wordcloudService::saveOrUpdateBatch);

            System.out.println("--------线程结束-------- : " + Thread.currentThread().getName());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

//    public void saveWordAndSynonyms(String word, long fruitId) {
//        try {
//            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//            String today = sdf.format(new Date());
//            List<WordcloudPo> wordcloudPoList = new ArrayList<>();
//            List<WordSynonymsPo> wordSynonymsPoList = new ArrayList<>();
//            WordcloudPo po = new WordcloudPo();
//            word = word.replace("。", "");
//            po.setFruitId(fruitId);
//            po.setName(word);
//            po.setCreateTime(new Date());
//            po.setUpdateTime(new Date());
//            System.out.println("======生成关键词insert语句======：" + "INSERT INTO `nd_word_cloud`(`create_time`, `fruit_id`, `name`, `update_time`) VALUES ('" + today + "', " + fruitId + ", '" + word + "', '" + today + "');");
//
//            wordcloudPoList.add(po);
//
//            List<String> synonymsResultList = null;
//            try {
//                synonymsResultList = chatGptUtil.getSynonyms(word);
//                if (!CollectionUtils.isEmpty(synonymsResultList)) {
//                    for (String synonyms : synonymsResultList) {
//                        WordSynonymsPo synonymsPo = new WordSynonymsPo();
//                        synonymsPo.setFruitId(fruitId);
//                        synonymsPo.setName(synonyms);
//                        synonymsPo.setCreateTime(new Date());
//                        synonymsPo.setUpdateTime(new Date());
//                        System.out.println("======生成近义词insert语句======：" + "INSERT INTO `nd_word_synonyms`(`create_time`, `fruit_id`, `name`, `update_time`) VALUES ('" + today + "', " + fruitId + ", '" + synonyms + "', '" + today + "');");
//
//                        wordSynonymsPoList.add(synonymsPo);
//                    }
//                }
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//            wordcloudService.saveOrUpdateBatch(wordcloudPoList);
//            wordSynonymsService.saveOrUpdateBatch(wordSynonymsPoList);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
//

    /**
     * 这里进行标注为异步任务，在执行此方法的时候，会单独开启线程来执行
     */
//    @Transactional
//    @Async("myExecutor")
    public void getFruitKeyWordsAndSave(List<Fruit> fruitList) {
        try {
            System.out.println("--------正在跑线程-------- : " + Thread.currentThread().getName());

            //所有成果作者
            List<FruitAuthor> fruitAuthorList = fruitAuthorService.list();
            //成果-作者名称连接 map
            Map<Long, String> fruitAuthorNamesMap = fruitAuthorList.stream().collect(
                    Collectors.groupingBy(FruitAuthor::getFruitId, Collectors.mapping(FruitAuthor::getAuthor, Collectors.toList()))
            ).entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, entry -> String.join(",", entry.getValue())));

            //成果-作者id连接 map
            Map<Long, String> fruitAuthorIdsMap = fruitAuthorList.stream().filter(fruitAuthor -> !ObjectUtils.isEmpty(fruitAuthor.getAuthor()))
                    .collect(
                            Collectors.groupingBy(FruitAuthor::getFruitId, Collectors.mapping(FruitAuthor::getOrgUserId, Collectors.toList()))
                    ).entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, entry -> entry.getValue().stream().map(String::valueOf).collect(Collectors.joining(","))));

            //国网关键词list
            List<FieldKeywordPo> gwFkList = fieldKeywordService.list();

            List<FruitFieldTagRel> fieldTagRelList = new ArrayList<>();
            Map<String, FruitFieldTagRel> fruitFieldTagRelMap = new HashMap<>();

            List<FruitFieldRel> fieldList = new ArrayList<>();

            fruitList.forEach(fruit -> {
                Set<String> gwFieldName = new HashSet<>();
                Set<String> gwFieldKeyword = new HashSet<>();

                String keywords = fruit.getKeyword();
                if(!ObjectUtils.isEmpty(keywords) && keywords.contains("[") && keywords.contains("]")){
                    List<String> resultList = JSON.parseArray(keywords, String.class);

                    //没有设置过关键词
                    if (!CollectionUtils.isEmpty(resultList)) {
                        for (String word : resultList) {
                            if(!ObjectUtils.isEmpty(word)){
//                            WordcloudPo po = new WordcloudPo();
                                String newWord = word.replace("。", "");
//                            po.setFruitId(fruit.getId());
//                            po.setName(newWord);
//                            po.setCreateTime(new Date());
//                            po.setUpdateTime(new Date());
//                            wordcloudPoList.add(po);

                                //根据成果关键词 匹配国网关键词
                                List<FieldKeywordPo> matchList  = gwFkList.stream().filter(fieldKeywordPo -> fieldKeywordPo.getName().contains(newWord)).collect(Collectors.toList());
                                if(!ObjectUtils.isEmpty(matchList)){
                                    matchList.forEach(fieldKeywordPo -> {
                                        if(fruitFieldTagRelMap.get(fruit.getId()+fieldKeywordPo.getName()) == null){
                                            FruitFieldTagRel fieldTagRel = new FruitFieldTagRel(fruit.getId(), newWord, fieldKeywordPo.getResearchField(), fieldKeywordPo.getName());
                                            fruitFieldTagRelMap.put(fruit.getId()+fieldKeywordPo.getName(), fieldTagRel);
                                            fieldTagRelList.add(fieldTagRel);
                                        }
                                    });
                                }else{
                                    FruitFieldTagRel fieldTagRel = new FruitFieldTagRel(fruit.getId(), newWord, "", "");
                                    fruitFieldTagRelMap.put(fruit.getId()+newWord, fieldTagRel);
                                    fieldTagRelList.add(fieldTagRel);
                                }

                                gwFieldName.addAll(matchList.stream().map(FieldKeywordPo::getResearchField).collect(Collectors.toSet()));
                                gwFieldKeyword.addAll(matchList.stream().map(FieldKeywordPo::getName).collect(Collectors.toSet()));
                            }
                        }
                    }
                    FruitFieldRel fruitFieldRel = new FruitFieldRel(fruit.getId(), fruit.getName(), !ObjectUtils.isEmpty(resultList) ? String.join(",", resultList) : "",
                            fruitAuthorNamesMap.get(fruit.getId()),  fruitAuthorIdsMap.get(fruit.getId()), fruit.getType(),fruit.getPubTime(), fruit.getSummary(),
                            !ObjectUtils.isEmpty(gwFieldName) ? String.join(",", gwFieldName) : "",
                            !ObjectUtils.isEmpty(gwFieldKeyword) ? String.join(",", gwFieldKeyword) : "");
                    fieldList.add(fruitFieldRel);
                }
            });

            System.out.println("-------共处理成果领域值及领域关键词-----" + fieldList.size() + "----个----");
            batchProcess(fieldList, fruitFieldRelService::saveOrUpdateBatch);

            System.out.println("-------共处理成果与国网关键词关系-----" + fieldTagRelList.size() + "----个----");
            batchProcess(fieldTagRelList, fruitFieldTagRelService::saveOrUpdateBatch);

//            System.out.println("-------共读取到关键词-----" + wordcloudPoList.size() + "----个----");
//            batchProcess(wordcloudPoList, wordcloudService::saveOrUpdateBatch);

            System.out.println("--------线程结束-------- : " + Thread.currentThread().getName());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @Transactional
//    @Async("myExecutor")
    public void dealTalentFields() {
        List<TalentBasic> talentBasicList = basicService.list();
        List<FruitResultModel> resultModels = fruitFieldRelService.getFruitKeyWord();
        //国网关键词云--用于查询专家
        Map<String, String> talentGwFkeyWordMap = resultModels.stream().collect(Collectors.toMap(FruitResultModel::getId, FruitResultModel::getGwFieldKeyword));
        Map<String, String> talentGwFNameMap = resultModels.stream().collect(Collectors.toMap(FruitResultModel::getId, FruitResultModel::getResearchField));
        Map<String, String> talentGwFruitKwMap = resultModels.stream().collect(Collectors.toMap(FruitResultModel::getId, FruitResultModel::getFruitKeyword));

        // 查询专家的成果
        List<FruitResultModel> fruitIdAndAuthorIdList = fruitService.getFruitIdGroupByAuthorId();
        Map<String, String> fruitIdAndAuthorIdMap = fruitIdAndAuthorIdList.stream().collect(Collectors.toMap(FruitResultModel::getOrgUserId, FruitResultModel::getId));

        List<TalentFieldRel> updateTalentFieldRelList = new ArrayList<>();
        int index = 0;
        // 更新专家领域词
        for (TalentBasic talentBasic : talentBasicList) {
            if(!ObjectUtils.isEmpty(talentBasic.getRealName())){
                index++;
                System.out.println("-------共有-----" + talentBasicList.size()+"---个专家----开始处理第---" + index + "---个---姓名---" + talentBasic.getRealName());
                List<String> gwFieldKeywordList = new ArrayList<>();
                List<String> gwFieldNameList = new ArrayList<>();
                List<String> fruitKeywordList = new ArrayList<>();
                if (!ObjectUtils.isEmpty(fruitIdAndAuthorIdMap.get(talentBasic.getId()))){
                    // 查询专家的成果
                    String[] fruitIds = fruitIdAndAuthorIdMap.get(talentBasic.getId()).split(",");
                    if (!ObjectUtils.isEmpty(fruitIds)) {
                        for (String fruitId : fruitIds) {
                            if(!ObjectUtils.isEmpty(talentGwFkeyWordMap.get(fruitId))){
                                gwFieldKeywordList.addAll(Arrays.asList(talentGwFkeyWordMap.get(fruitId).split(",")));
                            }
                            if(!ObjectUtils.isEmpty(talentGwFNameMap.get(fruitId))){
                                gwFieldNameList.addAll(Arrays.asList(talentGwFNameMap.get(fruitId).split(",")));
                            }
                            if(!ObjectUtils.isEmpty(talentGwFruitKwMap.get(fruitId))){
                                fruitKeywordList.addAll(Arrays.asList(talentGwFruitKwMap.get(fruitId).split(",")));
                            }
                        }
                    }
                }
                System.out.println("-------keywordSet长度=====" + gwFieldKeywordList.size());
                TalentFieldRel talentFieldRel = new TalentFieldRel(talentBasic.getId(), talentBasic.getRealName(),
                        talentBasic.getAllPhoneNumber(), talentBasic.getPartPhoneNumber(), talentBasic.getMail(),
                        talentBasic.getCooperationNum(), talentBasic.getCompany(), talentBasic.getType(), talentBasic.getProfilePhoto(),
                        !ObjectUtils.isEmpty(gwFieldNameList) ? getTopFrequencyStrings(gwFieldNameList) : "",
                        !ObjectUtils.isEmpty(gwFieldKeywordList) ? getTopFrequencyStrings(gwFieldKeywordList) : "",
                        !ObjectUtils.isEmpty(fruitKeywordList) ? getTopFrequencyStrings(fruitKeywordList) : "");
                updateTalentFieldRelList.add(talentFieldRel);
            }
        }

        System.out.println("--共获取到--"+updateTalentFieldRelList.size()+"---个关键词-----开始导入专家领域关键词-----");
        batchProcess(updateTalentFieldRelList, talentFieldRelService::saveOrUpdateBatch);
    }

    //list中出现次数最多的取前2
    private String getTopFrequencyStrings(List<String> strList){
        int size = 2;
        // 统计每个字符串的出现次数
        Map<String, Long> frequencyMap = strList.stream()
                .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));

        // 找出出现次数最多的前5个字符串
        List<String> topFrequentStrings = frequencyMap.entrySet().stream()
                .sorted(Map.Entry.<String, Long>comparingByValue().reversed())
                .limit(size)
                .map(Map.Entry::getKey)
                .collect(Collectors.toList());

        return String.join(",", topFrequentStrings);
    }

    public void dealFruitNewField() {
        String fieldArrStr = "[\n" +
                "    {\n" +
                "        \"researchField\":\"碳达峰碳中和研究\",\n" +
                "        \"researchDirections\":[\n" +
                "            {\n" +
                "                \"name\":\"双碳战略路径\",\n" +
                "                \"sqlItems\":[\n" +
                "                    \"碳+战略\",\n" +
                "                    \"碳+路径\"\n" +
                "                ],\n" +
                "                \"keywords\":[\n" +
                "                    \"双碳战略\",\n" +
                "                    \"双碳路径\"\n" +
                "                ]\n" +
                "            },\n" +
                "            {\n" +
                "                \"name\":\"双碳技术及政策\",\n" +
                "                \"sqlItems\":[\n" +
                "                    \"碳+技术\",\n" +
                "                    \"碳+政策\"\n" +
                "                ],\n" +
                "                \"keywords\":[\n" +
                "                    \"双碳技术\",\n" +
                "                    \"双碳政策\"\n" +
                "                ]\n" +
                "            },\n" +
                "            {\n" +
                "                \"name\":\"碳核算与碳管理\",\n" +
                "                \"sqlItems\":[\n" +
                "                    \"碳+核算\",\n" +
                "                    \"碳+管理\"\n" +
                "                ],\n" +
                "                \"keywords\":[\n" +
                "                    \"碳核算\",\n" +
                "                    \"碳管理\"\n" +
                "                ]\n" +
                "            },\n" +
                "            {\n" +
                "                \"name\":\"碳市场与碳价格\",\n" +
                "                \"sqlItems\":[\n" +
                "                    \"碳+市场\",\n" +
                "                    \"碳+价格\"\n" +
                "                ],\n" +
                "                \"keywords\":[\n" +
                "                    \"碳市场\",\n" +
                "                    \"碳价格\"\n" +
                "                ]\n" +
                "            },\n" +
                "            {\n" +
                "                \"name\":\"新能源发展\",\n" +
                "                \"sqlItems\":[\n" +
                "                    \"碳+新能源\"\n" +
                "                ],\n" +
                "                \"keywords\":[\n" +
                "                    \"新能源\"\n" +
                "                ]\n" +
                "            },\n" +
                "            {\n" +
                "                \"name\":\"节能与电气化\",\n" +
                "                \"sqlItems\":[\n" +
                "                    \"碳+电气化\",\n" +
                "                    \"碳+节能\"\n" +
                "                ],\n" +
                "                \"keywords\":[\n" +
                "                    \"双碳电气化\",\n" +
                "                    \"双碳节能\"\n" +
                "                ]\n" +
                "            },\n" +
                "            {\n" +
                "                \"name\":\"能源电力生态环境及循环经济\",\n" +
                "                \"sqlItems\":[\n" +
                "                    \"碳+生态\",\n" +
                "                    \"碳+循环经济\"\n" +
                "                ],\n" +
                "                \"keywords\":[\n" +
                "                    \"双碳生态\",\n" +
                "                    \"双碳循环经济\"\n" +
                "                ]\n" +
                "            }\n" +
                "        ]\n" +
                "    },\n" +
                "    {\n" +
                "        \"researchField\":\"新型电力系统研究\",\n" +
                "        \"researchDirections\":[\n" +
                "            {\n" +
                "                \"name\":\"新型电力系统战略规划\",\n" +
                "                \"sqlItems\":[\n" +
                "                    \"新型电力+战略\",\n" +
                "                    \"新型电力+规划\"\n" +
                "                ],\n" +
                "                \"keywords\":[\n" +
                "                    \"新型电力战略\",\n" +
                "                    \"新型电力规划\"\n" +
                "                ]\n" +
                "            },\n" +
                "            {\n" +
                "                \"name\":\"新型电力系统安全与综合评价\",\n" +
                "                \"sqlItems\":[\n" +
                "                    \"新型电力+安全\",\n" +
                "                    \"新型电力+评价\"\n" +
                "                ],\n" +
                "                \"keywords\":[\n" +
                "                    \"新型电力安全\",\n" +
                "                    \"新型电力评价\"\n" +
                "                ]\n" +
                "            },\n" +
                "            {\n" +
                "                \"name\":\"分布式新能源发展\",\n" +
                "                \"sqlItems\":[\n" +
                "                    \"分布式+电\"\n" +
                "                ],\n" +
                "                \"keywords\":[\n" +
                "                    \"分布式电\",\n" +
                "                    \"分布式能源\"\n" +
                "                ]\n" +
                "            },\n" +
                "            {\n" +
                "                \"name\":\"新型储能发展\",\n" +
                "                \"sqlItems\":[\n" +
                "                    \"储能+新\"\n" +
                "                ],\n" +
                "                \"keywords\":[\n" +
                "                    \"储能发展\",\n" +
                "                    \"新储能\"\n" +
                "                ]\n" +
                "            },\n" +
                "            {\n" +
                "                \"name\":\"城市能源基础设施与电网发展\",\n" +
                "                \"sqlItems\":[\n" +
                "                    \"城市+电网\",\n" +
                "                    \"城市+设施\"\n" +
                "                ],\n" +
                "                \"keywords\":[\n" +
                "                    \"城市电网\",\n" +
                "                    \"城市设施\"\n" +
                "                ]\n" +
                "            },\n" +
                "            {\n" +
                "                \"name\":\"农村能源基础设施与电网发展\",\n" +
                "                \"sqlItems\":[\n" +
                "                    \"农村+电\"\n" +
                "                ],\n" +
                "                \"keywords\":[\n" +
                "                    \"农村电网\"\n" +
                "                ]\n" +
                "            },\n" +
                "            {\n" +
                "                \"name\":\"电网形态演变及数智化转型\",\n" +
                "                \"sqlItems\":[\n" +
                "                    \"电网+形态\",\n" +
                "                    \"电网+智能+新\"\n" +
                "                ],\n" +
                "                \"keywords\":[\n" +
                "                    \"电网形态\",\n" +
                "                    \"电网智能\"\n" +
                "                ]\n" +
                "            },\n" +
                "            {\n" +
                "                \"name\":\"能源互联网新技术新业态新模式\",\n" +
                "                \"sqlItems\":[\n" +
                "                    \"能源+互联网\"\n" +
                "                ],\n" +
                "                \"keywords\":[\n" +
                "                    \"能源互联网\"\n" +
                "                ]\n" +
                "            },\n" +
                "            {\n" +
                "                \"name\":\"能源与电力价格\",\n" +
                "                \"sqlItems\":[\n" +
                "                    \"价格+能源\",\n" +
                "                    \"价格+电力\"\n" +
                "                ],\n" +
                "                \"keywords\":[\n" +
                "                    \"能源价格\",\n" +
                "                    \"能源电力\"\n" +
                "                ]\n" +
                "            }\n" +
                "        ]\n" +
                "    },\n" +
                "    {\n" +
                "        \"researchField\":\"能源电力战略规划研究\",\n" +
                "        \"researchDirections\":[\n" +
                "            {\n" +
                "                \"name\":\"能源战略及推演\",\n" +
                "                \"sqlItems\":[\n" +
                "                    \"能源+战略\",\n" +
                "                    \"能源+推演\"\n" +
                "                ],\n" +
                "                \"keywords\":[\n" +
                "                    \"能源战略\",\n" +
                "                    \"能源推演\"\n" +
                "                ]\n" +
                "            },\n" +
                "            {\n" +
                "                \"name\":\"国际能源发展与治理\",\n" +
                "                \"sqlItems\":[\n" +
                "                    \"国际+能源\"\n" +
                "                ],\n" +
                "                \"keywords\":[\n" +
                "                    \"国际能源\"\n" +
                "                ]\n" +
                "            },\n" +
                "            {\n" +
                "                \"name\":\"能源电力规划\",\n" +
                "                \"sqlItems\":[\n" +
                "                    \"能源+电+规划\"\n" +
                "                ],\n" +
                "                \"keywords\":[\n" +
                "                    \"能源规划\",\n" +
                "                    \"电规划\"\n" +
                "                ]\n" +
                "            },\n" +
                "            {\n" +
                "                \"name\":\"源网荷储协调发展\",\n" +
                "                \"sqlItems\":[\n" +
                "                    \"源+荷储\"\n" +
                "                ],\n" +
                "                \"keywords\":[\n" +
                "                    \"源荷储\"\n" +
                "                ]\n" +
                "            },\n" +
                "            {\n" +
                "                \"name\":\"电力产业链供应链\",\n" +
                "                \"sqlItems\":[\n" +
                "                    \"电+产业链\",\n" +
                "                    \"电+供应链\"\n" +
                "                ],\n" +
                "                \"keywords\":[\n" +
                "                    \"电力产业链\",\n" +
                "                    \"电力供应链\"\n" +
                "                ]\n" +
                "            },\n" +
                "            {\n" +
                "                \"name\":\"电力技术经济与投入产出效益\",\n" +
                "                \"sqlItems\":[\n" +
                "                    \"电+投入+效益\",\n" +
                "                    \"电+技术+经济\"\n" +
                "                ],\n" +
                "                \"keywords\":[\n" +
                "                    \"投入效益\",\n" +
                "                    \"技术经济\"\n" +
                "                ]\n" +
                "            },\n" +
                "            {\n" +
                "                \"name\":\"新能源发展\",\n" +
                "                \"sqlItems\":[\n" +
                "                    \"电+新能源\"\n" +
                "                ],\n" +
                "                \"keywords\":[\n" +
                "                    \"新能源\"\n" +
                "                ]\n" +
                "            },\n" +
                "            {\n" +
                "                \"name\":\"能源电力生态环境及循环经济\",\n" +
                "                \"sqlItems\":[\n" +
                "                    \"碳+生态\",\n" +
                "                    \"碳+循环经济\"\n" +
                "                ],\n" +
                "                \"keywords\":[\n" +
                "                    \"双碳生态\",\n" +
                "                    \"双碳循环经济\"\n" +
                "                ]\n" +
                "            },\n" +
                "            {\n" +
                "                \"name\":\"能源与电力价格\",\n" +
                "                \"sqlItems\":[\n" +
                "                    \"价格+能源\",\n" +
                "                    \"价格+电力\"\n" +
                "                ],\n" +
                "                \"keywords\":[\n" +
                "                    \"能源价格\",\n" +
                "                    \"能源电力\"\n" +
                "                ]\n" +
                "            }\n" +
                "        ]\n" +
                "    },\n" +
                "    {\n" +
                "        \"researchField\":\"能源电力供需研究\",\n" +
                "        \"researchDirections\":[\n" +
                "            {\n" +
                "                \"name\":\"经济能源分析预测\",\n" +
                "                \"sqlItems\":[\n" +
                "                    \"能源+预测\"\n" +
                "                ],\n" +
                "                \"keywords\":[\n" +
                "                    \"能源预测\"\n" +
                "                ]\n" +
                "            },\n" +
                "            {\n" +
                "                \"name\":\"能源供需分析\",\n" +
                "                \"sqlItems\":[\n" +
                "                    \"能源+供需\"\n" +
                "                ],\n" +
                "                \"keywords\":[\n" +
                "                    \"能源供需\"\n" +
                "                ]\n" +
                "            },\n" +
                "            {\n" +
                "                \"name\":\"电力供需分析\",\n" +
                "                \"sqlItems\":[\n" +
                "                    \"电+供需\"\n" +
                "                ],\n" +
                "                \"keywords\":[\n" +
                "                    \"电力供需\"\n" +
                "                ]\n" +
                "            },\n" +
                "            {\n" +
                "                \"name\":\"需求侧资源与负荷管理\",\n" +
                "                \"sqlItems\":[\n" +
                "                    \"电+需求侧\"\n" +
                "                ],\n" +
                "                \"keywords\":[\n" +
                "                    \"需求侧\"\n" +
                "                ]\n" +
                "            },\n" +
                "            {\n" +
                "                \"name\":\"节能与电气化\",\n" +
                "                \"sqlItems\":[\n" +
                "                    \"能源+电气化\",\n" +
                "                    \"能源+节能\"\n" +
                "                ],\n" +
                "                \"keywords\":[\n" +
                "                    \"能源电气化\",\n" +
                "                    \"能源节能\"\n" +
                "                ]\n" +
                "            }\n" +
                "        ]\n" +
                "    },\n" +
                "    {\n" +
                "        \"researchField\":\"能源电力体制改革与电力市场研究\",\n" +
                "        \"researchDirections\":[\n" +
                "            {\n" +
                "                \"name\":\"能源电力体制改革\",\n" +
                "                \"sqlItems\":[\n" +
                "                    \"电+体制改革\"\n" +
                "                ],\n" +
                "                \"keywords\":[\n" +
                "                    \"体制改革\"\n" +
                "                ]\n" +
                "            },\n" +
                "            {\n" +
                "                \"name\":\"能源及电力市场\",\n" +
                "                \"sqlItems\":[\n" +
                "                    \"市场+能源\",\n" +
                "                    \"市场+电力\"\n" +
                "                ],\n" +
                "                \"keywords\":[\n" +
                "                    \"市场能源\",\n" +
                "                    \"市场电力\"\n" +
                "                ]\n" +
                "            },\n" +
                "            {\n" +
                "                \"name\":\"能源与电力价格\",\n" +
                "                \"sqlItems\":[\n" +
                "                    \"价格+能源\",\n" +
                "                    \"价格+电力\"\n" +
                "                ],\n" +
                "                \"keywords\":[\n" +
                "                    \"能源价格\",\n" +
                "                    \"能源电力\"\n" +
                "                ]\n" +
                "            },\n" +
                "            {\n" +
                "                \"name\":\"国资国企改革与监管\",\n" +
                "                \"sqlItems\":[\n" +
                "                    \"改革+国有\",\n" +
                "                    \"改革+国企\",\n" +
                "                    \"监管+国有\",\n" +
                "                    \"监管+国企\"\n" +
                "                ],\n" +
                "                \"keywords\":[\n" +
                "                    \"改革国有\",\n" +
                "                    \"改革国企\",\n" +
                "                    \"监管国有\",\n" +
                "                    \"监管国企\"\n" +
                "                ]\n" +
                "            }\n" +
                "        ]\n" +
                "    },\n" +
                "    {\n" +
                "        \"researchField\":\"能源电力数字经济研究\",\n" +
                "        \"researchDirections\":[\n" +
                "            {\n" +
                "                \"name\":\"能源电力数字化发展\",\n" +
                "                \"sqlItems\":[\n" +
                "                    \"数字化+电\",\n" +
                "                    \"数字化+能源\",\n" +
                "                    \"能源大数据\",\n" +
                "                    \"数据要素\",\n" +
                "                    \"数字经济\"\n" +
                "                ],\n" +
                "                \"keywords\":[\n" +
                "                    \"数字化能源\",\n" +
                "                    \"能源大数据\",\n" +
                "                    \"数字经济\"\n" +
                "                ]\n" +
                "            },\n" +
                "            {\n" +
                "                \"name\":\"企业数字化转型\",\n" +
                "                \"sqlItems\":[\n" +
                "                    \"数字化转型\"\n" +
                "                ],\n" +
                "                \"keywords\":[\n" +
                "                    \"数字化转型\"\n" +
                "                ]\n" +
                "            }\n" +
                "        ]\n" +
                "    },\n" +
                "    {\n" +
                "        \"researchField\":\"企业战略规划研究\",\n" +
                "        \"researchDirections\":[\n" +
                "            {\n" +
                "                \"name\":\"企业发展战略\",\n" +
                "                \"sqlItems\":[\n" +
                "                    \"企业+战略\"\n" +
                "                ],\n" +
                "                \"keywords\":[\n" +
                "                    \"企业战略\"\n" +
                "                ]\n" +
                "            },\n" +
                "            {\n" +
                "                \"name\":\"企业发展规划\",\n" +
                "                \"sqlItems\":[\n" +
                "                    \"企业+规划\"\n" +
                "                ],\n" +
                "                \"keywords\":[\n" +
                "                    \"企业规划\"\n" +
                "                ]\n" +
                "            },\n" +
                "            {\n" +
                "                \"name\":\"企业国际化发展\",\n" +
                "                \"sqlItems\":[\n" +
                "                    \"企业+国际化\"\n" +
                "                ],\n" +
                "                \"keywords\":[\n" +
                "                    \"企业国际化\"\n" +
                "                ]\n" +
                "            },\n" +
                "            {\n" +
                "                \"name\":\"企业产业发展\",\n" +
                "                \"sqlItems\":[\n" +
                "                    \"企业+产业\"\n" +
                "                ],\n" +
                "                \"keywords\":[\n" +
                "                    \"企业产业\"\n" +
                "                ]\n" +
                "            }\n" +
                "        ]\n" +
                "    },\n" +
                "    {\n" +
                "        \"researchField\":\"企业经营与财务研究\",\n" +
                "        \"researchDirections\":[\n" +
                "            {\n" +
                "                \"name\":\"公司综合计划、预算与经济活动分析\",\n" +
                "                \"sqlItems\":[\n" +
                "                    \"综合计划\",\n" +
                "                    \"经济活动分析\",\n" +
                "                    \"企业+预算\"\n" +
                "                ],\n" +
                "                \"keywords\":[\n" +
                "                    \"综合计划\",\n" +
                "                    \"经济活动分析\",\n" +
                "                    \"企业预算\"\n" +
                "                ]\n" +
                "            },\n" +
                "            {\n" +
                "                \"name\":\"企业投融资与资本运营\",\n" +
                "                \"sqlItems\":[\n" +
                "                    \"企业+投资\",\n" +
                "                    \"企业+融资\",\n" +
                "                    \"资本运营\"\n" +
                "                ],\n" +
                "                \"keywords\":[\n" +
                "                    \"企业投资\",\n" +
                "                    \"企业融资\",\n" +
                "                    \"资本运营\"\n" +
                "                ]\n" +
                "            },\n" +
                "            {\n" +
                "                \"name\":\"公司电力价格\",\n" +
                "                \"sqlItems\":[\n" +
                "                    \"电力+价格\"\n" +
                "                ],\n" +
                "                \"keywords\":[\n" +
                "                    \"电力价格\"\n" +
                "                ]\n" +
                "            },\n" +
                "            {\n" +
                "                \"name\":\"公司财务管理\",\n" +
                "                \"sqlItems\":[\n" +
                "                    \"财务管理\"\n" +
                "                ],\n" +
                "                \"keywords\":[\n" +
                "                    \"财务管理\"\n" +
                "                ]\n" +
                "            },\n" +
                "            {\n" +
                "                \"name\":\"能源金融与产业金融发展\",\n" +
                "                \"sqlItems\":[\n" +
                "                    \"金融+能源\",\n" +
                "                    \"金融+产业\"\n" +
                "                ],\n" +
                "                \"keywords\":[\n" +
                "                    \"金融能源\",\n" +
                "                    \"金融产业\"\n" +
                "                ]\n" +
                "            },\n" +
                "            {\n" +
                "                \"name\":\"公司审计\",\n" +
                "                \"sqlItems\":[\n" +
                "                    \"审计+公司\",\n" +
                "                    \"审计+企业\"\n" +
                "                ],\n" +
                "                \"keywords\":[\n" +
                "                    \"审计公司\",\n" +
                "                    \"审计企业\"\n" +
                "                ]\n" +
                "            }\n" +
                "        ]\n" +
                "    },\n" +
                "    {\n" +
                "        \"researchField\":\"企业运营管理研究\",\n" +
                "        \"researchDirections\":[\n" +
                "            {\n" +
                "                \"name\":\"企业绩效管理\",\n" +
                "                \"sqlItems\":[\n" +
                "                    \"绩效+公司\",\n" +
                "                    \"绩效+企业\"\n" +
                "                ],\n" +
                "                \"keywords\":[\n" +
                "                    \"绩效公司\",\n" +
                "                    \"绩效企业\"\n" +
                "                ]\n" +
                "            },\n" +
                "            {\n" +
                "                \"name\":\"企业人力资源管理\",\n" +
                "                \"sqlItems\":[\n" +
                "                    \"人力资源+公司\",\n" +
                "                    \"人力资源+企业\"\n" +
                "                ],\n" +
                "                \"keywords\":[\n" +
                "                    \"公司人力资源\",\n" +
                "                    \"企业人力资源\"\n" +
                "                ]\n" +
                "            },\n" +
                "            {\n" +
                "                \"name\":\"企业对标和世界一流评价\",\n" +
                "                \"sqlItems\":[\n" +
                "                    \"对标+公司\",\n" +
                "                    \"对标+企业\",\n" +
                "                    \"世界+公司\",\n" +
                "                    \"世界+企业\"\n" +
                "                ],\n" +
                "                \"keywords\":[\n" +
                "                    \"对标公司\",\n" +
                "                    \"对标企业\",\n" +
                "                    \"世界公司\",\n" +
                "                    \"世界企业\"\n" +
                "                ]\n" +
                "            },\n" +
                "            {\n" +
                "                \"name\":\"企业科技战略与创新管理\",\n" +
                "                \"sqlItems\":[\n" +
                "                    \"战略+公司\",\n" +
                "                    \"战略+企业\",\n" +
                "                    \"创新+公司\",\n" +
                "                    \"创新+企业\"\n" +
                "                ],\n" +
                "                \"keywords\":[\n" +
                "                    \"战略公司\",\n" +
                "                    \"战略企业\",\n" +
                "                    \"创新公司\",\n" +
                "                    \"创新企业\"\n" +
                "                ]\n" +
                "            },\n" +
                "            {\n" +
                "                \"name\":\"电网资产全寿命管理\",\n" +
                "                \"sqlItems\":[\n" +
                "                    \"资产+寿命\"\n" +
                "                ],\n" +
                "                \"keywords\":[\n" +
                "                    \"资产寿命\"\n" +
                "                ]\n" +
                "            }\n" +
                "        ]\n" +
                "    },\n" +
                "    {\n" +
                "        \"researchField\":\"企业治理与风控合规研究\",\n" +
                "        \"researchDirections\":[\n" +
                "            {\n" +
                "                \"name\":\"企业大监督体系建设\",\n" +
                "                \"sqlItems\":[\n" +
                "                    \"监督+公司\",\n" +
                "                    \"监督+企业\"\n" +
                "                ],\n" +
                "                \"keywords\":[\n" +
                "                    \"监督公司\",\n" +
                "                    \"监督企业\"\n" +
                "                ]\n" +
                "            },\n" +
                "            {\n" +
                "                \"name\":\"企业制度与治理结构\",\n" +
                "                \"sqlItems\":[\n" +
                "                    \"制度+公司\",\n" +
                "                    \"制度+企业\",\n" +
                "                    \"治理+公司\",\n" +
                "                    \"治理+企业\"\n" +
                "                ],\n" +
                "                \"keywords\":[\n" +
                "                    \"制度公司\",\n" +
                "                    \"制度企业\",\n" +
                "                    \"治理公司\",\n" +
                "                    \"治理企业\"\n" +
                "                ]\n" +
                "            },\n" +
                "            {\n" +
                "                \"name\":\"企业全面风险管理与内控\",\n" +
                "                \"sqlItems\":[\n" +
                "                    \"风险+管理+公司\",\n" +
                "                    \"风险+管理+企业\",\n" +
                "                    \"内控+公司\",\n" +
                "                    \"内控+企业\"\n" +
                "                ],\n" +
                "                \"keywords\":[\n" +
                "                    \"风险管理\",\n" +
                "                    \"内控\"\n" +
                "                ]\n" +
                "            },\n" +
                "            {\n" +
                "                \"name\":\"企业法律合规管理\",\n" +
                "                \"sqlItems\":[\n" +
                "                    \"法律\"\n" +
                "                ],\n" +
                "                \"keywords\":[\n" +
                "                    \"法律\"\n" +
                "                ]\n" +
                "            }\n" +
                "        ]\n" +
                "    },\n" +
                "    {\n" +
                "        \"researchField\":\"品牌文化与社会责任研究\",\n" +
                "        \"researchDirections\":[\n" +
                "            {\n" +
                "                \"name\":\"企业文化\",\n" +
                "                \"sqlItems\":[\n" +
                "                    \"文化+公司\",\n" +
                "                    \"文化+企业\"\n" +
                "                ],\n" +
                "                \"keywords\":[\n" +
                "                    \"文化公司\",\n" +
                "                    \"文化企业\"\n" +
                "                ]\n" +
                "            },\n" +
                "            {\n" +
                "                \"name\":\"企业品牌\",\n" +
                "                \"sqlItems\":[\n" +
                "                    \"品牌+公司\",\n" +
                "                    \"品牌+企业\"\n" +
                "                ],\n" +
                "                \"keywords\":[\n" +
                "                    \"品牌公司\",\n" +
                "                    \"品牌企业\"\n" +
                "                ]\n" +
                "            },\n" +
                "            {\n" +
                "                \"name\":\"企业社会责任\",\n" +
                "                \"sqlItems\":[\n" +
                "                    \"责任+公司\",\n" +
                "                    \"责任+企业\"\n" +
                "                ],\n" +
                "                \"keywords\":[\n" +
                "                    \"责任公司\",\n" +
                "                    \"责任企业\"\n" +
                "                ]\n" +
                "            }\n" +
                "        ]\n" +
                "    },\n" +
                "    {\n" +
                "        \"researchField\":\"数据、模型工具及实验室\",\n" +
                "        \"researchDirections\":[\n" +
                "            {\n" +
                "                \"name\":\"数据、模型工具及实验室\",\n" +
                "                \"sqlItems\":[\n" +
                "                    \"实验室\",\n" +
                "                    \"模型+工具\",\n" +
                "                    \"数据+工具\"\n" +
                "                ],\n" +
                "                \"keywords\":[\n" +
                "                    \"实验室\",\n" +
                "                    \"模型工具\",\n" +
                "                    \"数据工具\"\n" +
                "                ]\n" +
                "            }\n" +
                "        ]\n" +
                "    }\n" +
                "]";
//        String fieldArrStr = "[{\"researchField\":\"碳达峰碳中和研究\",\"researchDirections\":[{\"name\":\"双碳战略路径\",\"sqlItems\":[\"碳+战略\",\"碳+路径\"],\"keywords\":[\"双碳战略\",\"双碳路径\"]},{\"name\":\"双碳技术及政策\",\"sqlItems\":[\"碳+技术\",\"碳+政策\"],\"keywords\":[\"双碳技术\",\"双碳政策\"]},{\"name\":\"碳核算与碳管理\",\"sqlItems\":[\"碳+核算\",\"碳+管理\"],\"keywords\":[\"碳核算\",\"碳管理\"]},{\"name\":\"碳市场与碳价格\",\"sqlItems\":[\"碳+市场\",\"碳+价格\"],\"keywords\":[\"碳市场\",\"碳价格\"]},{\"name\":\"新能源发展\",\"sqlItems\":[\"碳+新能源\"],\"keywords\":[\"新能源\"]},{\"name\":\"节能与电气化\",\"sqlItems\":[\"碳+电气化\",\"碳+节能\"],\"keywords\":[\"双碳电气化\",\"双碳节能\"]},{\"name\":\"能源电力生态环境及循环经济\",\"sqlItems\":[\"碳+生态\",\"碳+循环经济\"],\"keywords\":[\"双碳生态\",\"双碳循环经济\"]}]},{\"researchField\":\"新型电力系统研究\",\"researchDirections\":[{\"name\":\"新型电力系统战略规划\",\"sqlItems\":[\"新型电力+战略\",\"新型电力+规划\"],\"keywords\":[\"新型电力战略\",\"新型电力规划\"]},{\"name\":\"新型电力系统安全与综合评价\",\"sqlItems\":[\"新型电力+安全\",\"新型电力+评价\"],\"keywords\":[\"新型电力安全\",\"新型电力评价\"]},{\"name\":\"分布式新能源发展\",\"sqlItems\":[\"分布式+电\"],\"keywords\":[\"分布式电\",\"分布式能源\"]},{\"name\":\"新型储能发展\",\"sqlItems\":[\"储能+新\"],\"keywords\":[\"储能发展\",\"新储能\"]},{\"name\":\"城市能源基础设施与电网发展\",\"sqlItems\":[\"城市+电网\",\"城市+设施\"],\"keywords\":[\"城市电网\",\"城市设施\"]},{\"name\":\"农村能源基础设施与电网发展\",\"sqlItems\":[\"农村+电\"],\"keywords\":[\"农村电网\"]},{\"name\":\"电网形态演变及数智化转型\",\"sqlItems\":[\"电网+形态\",\"电网+智能+新\"],\"keywords\":[\"电网形态\",\"电网智能\"]},{\"name\":\"能源互联网新技术新业态新模式\",\"sqlItems\":[\"能源+互联网\"],\"keywords\":[\"能源互联网\"]},{\"name\":\"能源与电力价格\",\"sqlItems\":[\"价格+能源\",\"价格+电力\"],\"keywords\":[\"能源价格\",\"能源电力\"]}]},{\"researchField\":\"能源电力战略规划研究\",\"researchDirections\":[{\"name\":\"能源战略及推演\",\"sqlItems\":[\"能源+战略\",\"能源+推演\"],\"keywords\":[\"能源战略\",\"能源推演\"]},{\"name\":\"国际能源发展与治理\",\"sqlItems\":[\"国际+能源\"],\"keywords\":[\"国际能源\"]},{\"name\":\"能源电力规划\",\"sqlItems\":[\"能源+电+规划\"],\"keywords\":[\"能源规划\",\"电规划\"]},{\"name\":\"源网荷储协调发展\",\"sqlItems\":[\"源+荷储\"],\"keywords\":[\"源荷储\"]},{\"name\":\"电力产业链供应链\",\"sqlItems\":[\"电+产业链\",\"电+供应链\"],\"keywords\":[\"电力产业链\",\"电力供应链\"]},{\"name\":\"电力技术经济与投入产出效益\",\"sqlItems\":[\"电+投入+效益\",\"电+技术+经济\"],\"keywords\":[\"投入效益\",\"技术经济\"]},{\"name\":\"新能源发展\",\"sqlItems\":[\"电+新能源\"],\"keywords\":[\"新能源\"]},{\"name\":\"能源电力生态环境及循环经济\",\"sqlItems\":[\"碳+生态\",\"碳+循环经济\"],\"keywords\":[\"双碳生态\",\"双碳循环经济\"]},{\"name\":\"能源与电力价格\",\"sqlItems\":[\"价格+能源\",\"价格+电力\"],\"keywords\":[\"能源价格\",\"能源电力\"]}]},{\"researchField\":\"能源电力供需研究\",\"researchDirections\":[{\"name\":\"经济能源分析预测\",\"sqlItems\":[\"能源+预测\"],\"keywords\":[\"能源预测\"]},{\"name\":\"能源供需分析\",\"sqlItems\":[\"能源+供需\"],\"keywords\":[\"能源供需\"]},{\"name\":\"电力供需分析\",\"sqlItems\":[\"电+供需\"],\"keywords\":[\"电力供需\"]},{\"name\":\"需求侧资源与负荷管理\",\"sqlItems\":[\"电+需求侧\"],\"keywords\":[\"需求侧\"]},{\"name\":\"节能与电气化\",\"sqlItems\":[\"能源+电气化\",\"能源+节能\"],\"keywords\":[\"能源电气化\",\"能源节能\"]}]},{\"researchField\":\"能源电力体制改革与电力市场研究\",\"researchDirections\":[{\"name\":\"能源电力体制改革\",\"sqlItems\":[\"电+体制改革\"],\"keywords\":[\"体制改革\"]},{\"name\":\"能源及电力市场\",\"sqlItems\":[\"市场+能源\",\"市场+电力\"],\"keywords\":[\"市场能源\",\"市场电力\"]},{\"name\":\"能源与电力价格\",\"sqlItems\":[\"价格+能源\",\"价格+电力\"],\"keywords\":[\"能源价格\",\"能源电力\"]},{\"name\":\"国资国企改革与监管\",\"sqlItems\":[\"改革+国有\",\"改革+国企\",\"监管+国有\",\"监管+国企\"],\"keywords\":[\"改革国有\",\"改革国企\",\"监管国有\",\"监管国企\"]}]},{\"researchField\":\"能源电力数字经济研究\",\"researchDirections\":[{\"name\":\"能源电力数字化发展\",\"sqlItems\":[\"数字化+电\",\"数字化+能源\",\"能源大数据\",\"数据要素\",\"数字经济\"],\"keywords\":[\"数字化能源\",\"能源大数据\",\"数字经济\"]},{\"name\":\"企业数字化转型\",\"sqlItems\":[\"数字化转型\"],\"keywords\":[\"数字化转型\"]}]},{\"researchField\":\"企业战略规划研究\",\"researchDirections\":[{\"name\":\"企业发展战略\",\"sqlItems\":[\"企业+战略\"],\"keywords\":[\"企业战略\"]},{\"name\":\"企业发展规划\",\"sqlItems\":[\"企业+规划\"],\"keywords\":[\"企业规划\"]},{\"name\":\"企业国际化发展\",\"sqlItems\":[\"企业+国际化\"],\"keywords\":[\"企业国际化\"]},{\"name\":\"企业产业发展\",\"sqlItems\":[\"企业+产业\"],\"keywords\":[\"企业产业\"]}]},{\"researchField\":\"企业经营与财务研究\",\"researchDirections\":[{\"name\":\"公司综合计划、预算与经济活动分析\",\"sqlItems\":[\"综合计划\",\"经济活动分析\",\"企业+预算\"],\"keywords\":[\"综合计划\",\"经济活动分析\",\"企业预算\"]},{\"name\":\"企业投融资与资本运营\",\"sqlItems\":[\"企业+投资\",\"企业+融资\",\"资本运营\"],\"keywords\":[\"企业投资\",\"企业融资\",\"资本运营\"]},{\"name\":\"公司电力价格\",\"sqlItems\":[\"电力+价格\"],\"keywords\":[\"电力价格\"]},{\"name\":\"公司财务管理\",\"sqlItems\":[\"财务管理\"],\"keywords\":[\"财务管理\"]},{\"name\":\"能源金融与产业金融发展\",\"sqlItems\":[\"金融+能源\",\"金融+产业\"],\"keywords\":[\"金融能源\",\"金融产业\"]},{\"name\":\"公司审计\",\"sqlItems\":[\"审计+公司\",\"审计+企业\"],\"keywords\":[\"审计公司\",\"审计企业\"]}]},{\"researchField\":\"企业运营管理研究\",\"researchDirections\":[{\"name\":\"企业绩效管理\",\"sqlItems\":[\"绩效+公司\",\"绩效+企业\"],\"keywords\":[\"绩效公司\",\"绩效企业\"]},{\"name\":\"企业人力资源管理\",\"sqlItems\":[\"人力资源+公司\",\"人力资源+企业\"],\"keywords\":[\"公司人力资源\",\"企业人力资源\"]},{\"name\":\"企业对标和世界一流评价\",\"sqlItems\":[\"对标+公司\",\"对标+企业\",\"世界+公司\",\"世界+企业\"],\"keywords\":[\"对标公司\",\"对标企业\",\"世界公司\",\"世界企业\"]},{\"name\":\"企业科技战略与创新管理\",\"sqlItems\":[\"战略+公司\",\"战略+企业\",\"创新+公司\",\"创新+企业\"],\"keywords\":[\"战略公司\",\"战略企业\",\"创新公司\",\"创新企业\"]},{\"name\":\"电网资产全寿命管理\",\"sqlItems\":[\"资产+寿命\"],\"keywords\":[\"资产寿命\"]}]},{\"researchField\":\"企业治理与风控合规研究\",\"researchDirections\":[{\"name\":\"企业大监督体系建设\",\"sqlItems\":[\"监督+公司\",\"监督+企业\"],\"keywords\":[\"监督公司\",\"监督企业\"]},{\"name\":\"企业制度与治理结构\",\"sqlItems\":[\"制度+公司\",\"制度+企业\",\"治理+公司\",\"治理+企业\"],\"keywords\":[\"制度公司\",\"制度企业\",\"治理公司\",\"治理企业\"]},{\"name\":\"企业全面风险管理与内控\",\"sqlItems\":[\"风险+管理+公司\",\"风险+管理+企业\",\"内控+公司\",\"内控+企业\"],\"keywords\":[\"风险管理\",\"内控\"]},{\"name\":\"企业法律合规管理\",\"sqlItems\":[\"法律\"],\"keywords\":[\"法律\"]}]},{\"researchField\":\"品牌文化与社会责任研究\",\"researchDirections\":[{\"name\":\"企业文化\",\"sqlItems\":[\"文化+公司\",\"文化+企业\"],\"keywords\":[\"文化公司\",\"文化企业\"]},{\"name\":\"企业品牌\",\"sqlItems\":[\"品牌+公司\",\"品牌+企业\"],\"keywords\":[\"品牌公司\",\"品牌企业\"]},{\"name\":\"企业社会责任\",\"sqlItems\":[\"责任+公司\",\"责任+企业\"],\"keywords\":[\"责任公司\",\"责任企业\"]}]},{\"researchField\":\"数据、模型工具及实验室\",\"researchDirections\":[{\"name\":\"数据、模型工具及实验室\",\"sqlItems\":[\"实验室\",\"模型+工具\",\"数据+工具\"],\"keywords\":[\"实验室\",\"模型工具\",\"数据工具\"]}]}]";
        JSONArray jsonArray = JSONArray.parseArray(fieldArrStr);

        List<Fruit> fruitList = fruitService.list();

        // 遍历数组中的每个元素
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject fieldObject = jsonArray.getJSONObject(i);

            // 提取研究领域名称
            String researchField = fieldObject.getString("researchField");
            System.out.println("Research Field: " + researchField);

            //新增字典表 研究领域
            NdSysDict ndSysDict = new NdSysDict();
            ndSysDict.setParentId(123071605385521293L);
            ndSysDict.setIsDel(false);
            ndSysDict.setTypeKey("GwResearchField");
            ndSysDict.setDName(researchField);
            ndSysDict.setDValue((i + 1) + "");
            sysDictService.save(ndSysDict);


            // 提取研究方向详细信息
            JSONArray directionsArray = fieldObject.getJSONArray("researchDirections");
            for (int j = 0; j < directionsArray.size(); j++) {
                JSONObject directionObject = directionsArray.getJSONObject(j);

                // 提取研究方向名称
                String directionName = directionObject.getString("name");
                System.out.println("Direction Name: " + directionName);

                //新增字典表 研究方向
                NdSysDict ndSysDictDt = new NdSysDict();
                ndSysDictDt.setParentId(Long.parseLong(ndSysDict.getId()));
                ndSysDictDt.setIsDel(false);
                ndSysDictDt.setTypeKey("GwResearchDirection");
                ndSysDictDt.setDName(directionName);
                ndSysDictDt.setDValue((j + 1) + "");
                sysDictService.save(ndSysDictDt);

                // 提取 SQL 项
                JSONArray sqlItemsArray = directionObject.getJSONArray("sqlItems");
                System.out.println("SQL Items: " + sqlItemsArray);

                //查询成果
                QueryWrapper<Fruit> wsWrapper = new QueryWrapper();
                if (!CollectionUtils.isEmpty(sqlItemsArray)) {
                    for (Object sqlItemObj : sqlItemsArray) {
                        wsWrapper.or(w -> {
                            if (sqlItemObj.toString().contains("+")) {
                                String[] sqlItemArr = sqlItemObj.toString().split("\\+");
                                w.and(n -> {
                                    for (String item : sqlItemArr) {
                                        n.like("name", "%" + item + "%");
                                    }
                                });
                            } else {
                                w.like("name", "%" + sqlItemObj + "%");
                            }
                        });
                    }
                }
                // 提取关键字
                JSONArray keywordsArray = directionObject.getJSONArray("keywords");
                System.out.println("Keywords: " + keywordsArray);

                List<FruitFieldRel> fruitFieldRels = new ArrayList<>();
                List<WordcloudPo> wordcloudPos = new ArrayList<>();

                List<Fruit> wrapperFruitList = fruitMapper.selectList(wsWrapper);
                if (!CollectionUtils.isEmpty(wrapperFruitList)) {
                    wrapperFruitList.forEach(fruit -> {
                        //新增映射关系
//                        FruitField fruitField = new FruitField(fruit.getId(), ndSysDict.getDValue(), ndSysDictDt.getDValue(), null);
//                        fruitFields.add(fruitField);
//                        fruitFieldRelService.save(fruitField);

                        //新增成果关键词
                        if (!CollectionUtils.isEmpty(keywordsArray)) {
                            for (Object keywordObj : keywordsArray) {
                                WordcloudPo wordcloudPo = new WordcloudPo();
                                wordcloudPo.setResearchField(researchField);
                                wordcloudPo.setFruitId(fruit.getId());
                                wordcloudPo.setName(keywordObj.toString());
                                wordcloudPos.add(wordcloudPo);
//                                wordcloudService.save(wordcloudPo);
                            }
                        }
                    });
//                    fruitFieldRelService.saveBatch(fruitFields);
                    wordcloudService.saveBatch(wordcloudPos);
                }

            }
        }

    }

//    public static void main(String[] args) {
//        String json = "{ \"authors\": {\n" +
//                "            \"贺兴\": {\n" +
//                "                \"departments\": [\n" +
//                "                    \"电力传输与功率变换控制教育部重点实验室(上海交通大学)\"\n" +
//                "                ],\n" +
//                "                \"email\": \"hexing_hx@126.com\"\n" +
//                "            },\n" +
//                "            \"潘美琪\": {\n" +
//                "                \"departments\": [\n" +
//                "                    \"上海交通大学国家电投智慧能源创新学院\"\n" +
//                "                ],\n" +
//                "                \"email\": \"\"\n" +
//                "            },\n" +
//                "            \"艾芊\": {\n" +
//                "                \"departments\": [\n" +
//                "                    \"电力传输与功率变换控制教育部重点实验室(上海交通大学)\"\n" +
//                "                ],\n" +
//                "                \"email\": \"\"\n" +
//                "            }\n" +
//                "        }}";
//        JSONObject jsonObject = JSON.parseObject(json);
//        // 遍历authors对象
//        for (String key : jsonObject.getJSONObject("authors").keySet()) {
//            // 获取每个作者的departments数组
//            JSONArray departments = jsonObject.getJSONObject("authors").getJSONObject(key).getJSONArray("departments");
//            System.out.println(key + "--" + departments.get(0));
//        }
//
//    }

    public void dealGwFieldKeyWord(HttpServletRequest request) throws IOException {

        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
        for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
            MultipartFile multipartFile = entity.getValue();// 获取上传文件对象
            // 获取文件原始名称
            String originalFileName = multipartFile.getOriginalFilename();
            log.info("------------正在导入研究领域关键词--------" + originalFileName);
            // 创建临时文件
            assert originalFileName != null;
            File file = File.createTempFile("temp", originalFileName.substring(originalFileName.lastIndexOf(".")));
            // 将上传文件转为临时文件
            multipartFile.transferTo(file);
            // 退出时删除临时文件
            file.deleteOnExit();

            Workbook workbook = null;
            if (file.getPath().endsWith("xls")) {
                workbook = new HSSFWorkbook(new FileInputStream(file));
            } else if (file.getPath().endsWith("xlsx")) {
                workbook = new XSSFWorkbook(new FileInputStream(file));
            }
            // 获取第一个sheet
            Sheet sheet = workbook.getSheetAt(0);

            // 存储合并单元格的标题
            Map<Integer, String> mergedHeaders = new HashMap<>();
            // 存储要存储的数据
            List<FieldKeywordPo> fieldKeywordPoList = new ArrayList<>();

            for (int i = 1; i <= sheet.getLastRowNum(); i++) {
                Row row = sheet.getRow(i);
                if (row != null) {
                    // 检查第一列是否有单元格
                    Cell firstColumnCell = row.getCell(0);

                    String fieldValue = "";
                    if (firstColumnCell != null) {
                        // 如果单元格是合并单元格的一部分，获取合并单元格的标题
                        if (isMergedRegion(firstColumnCell, sheet)) {
                            fieldValue = getMergedHeader(firstColumnCell, sheet, mergedHeaders);
                        } else {
                            // 否则，直接获取单元格的值
                            fieldValue = firstColumnCell.getStringCellValue();
                        }
                    } else {
                        // 如果第一列没有单元格，可以设置默认值或跳过
                        fieldValue = "";
                    }

                    String directionValue = "";
                    // 获取第二列的值
                    Cell secondColumnCell = row.getCell(1);
                    if (secondColumnCell != null) {
                        directionValue = secondColumnCell.getStringCellValue();
                    }

                    String deptName = "";
                    // 获取第三列的值
                    Cell thirdColumnCell = row.getCell(2);
                    if (secondColumnCell != null) {
                        deptName = thirdColumnCell.getStringCellValue();
                    }

                    String keywords = "";
                    // 获取第四列的值
                    Cell fourColumnCell = row.getCell(3);
                    if (fourColumnCell != null) {
                        keywords = fourColumnCell.getStringCellValue();

                        String[] keywordsArr = keywords.split("、");
                        for (String kw : keywordsArr) {
                            FieldKeywordPo fieldKeywordPo = new FieldKeywordPo(fieldValue, directionValue, deptName, kw);
                            fieldKeywordPoList.add(fieldKeywordPo);
                        }
                    }
                }
            }

            workbook.close();

            int batchSize = 1000; // 每次处理的批次大小

            if(!CollectionUtils.isEmpty(fieldKeywordPoList)){
                for (int i = 0; i < fieldKeywordPoList.size(); i += batchSize) {
                    List<FieldKeywordPo> batch = fieldKeywordPoList.subList(i, Math.min(i + batchSize, fieldKeywordPoList.size()));
                    fieldKeywordService.saveOrUpdateBatch(batch);
                }
            }
            log.info("------------导入结束-----------");
        }
    }

    private static boolean isMergedRegion(Cell cell, Sheet sheet) {
        for (CellRangeAddress mergedRegion : sheet.getMergedRegions()) {
            if (mergedRegion.isInRange(cell.getRowIndex(), cell.getColumnIndex())) {
                return true;
            }
        }
        return false;
    }

    private static String getMergedHeader(Cell cell, Sheet sheet, Map<Integer, String> mergedHeaders) {
        int rowNum = cell.getRowIndex();
        if (!mergedHeaders.containsKey(rowNum)) {
            // 找到合并单元格的起始单元格并获取其值
            for (CellRangeAddress mergedRegion : sheet.getMergedRegions()) {
                if (mergedRegion.isInRange(rowNum, cell.getColumnIndex())) {
                    int startRow = mergedRegion.getFirstRow();
                    int startColumn = mergedRegion.getFirstColumn();
                    Cell startCell = sheet.getRow(startRow).getCell(startColumn);
                    mergedHeaders.put(rowNum, startCell.getStringCellValue());
                    break;
                }
            }
        }
        return mergedHeaders.get(rowNum);
    }

    //计算相似度
    public static boolean calculateSimilarity(String unit1, String unit2) {
//        double similarity = 0.5;
//        int levenshteinDistance =  org.apache.commons.lang3.StringUtils.getLevenshteinDistance(s1, s2);
//        int maxLength = Math.max(s1.length(), s2.length());
//        return (1.0 - (double) levenshteinDistance / maxLength) > similarity;
        LevenshteinDistance levenshteinDistance = LevenshteinDistance.getDefaultInstance();
        int distance = levenshteinDistance.apply(unit1, unit2);
        double similarity = 1 - (double) distance / Math.max(unit1.length(), unit2.length());

//        System.out.println("-----计算相似度----unit1---=: " + unit1 + "---unit2---=" + unit2);
//        System.out.println("Levenshtein距离: " + distance);
//        System.out.println("相似度: " + similarity);

        // 设置一个阈值，例如0.5，来决定是否认为两个单位是同一家
        if (similarity > 0.5) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 计算两个字符串的Jaccard相似度。
     *
     * @param s1 第一个字符串
     * @param s2 第二个字符串
     * @return Jaccard相似度
     */
    public static double jaccardSimilarity(String s1, String s2) {
        // 将字符串转换为字符集合
        Set<Character> set1 = new HashSet<>();
        Set<Character> set2 = new HashSet<>();
        for (char c : s1.toCharArray()) {
            set1.add(c);
        }
        for (char c : s2.toCharArray()) {
            set2.add(c);
        }

        // 计算交集
        Set<Character> intersection = new HashSet<>(set1);
        intersection.retainAll(set2);

        // 计算并集
        Set<Character> union = new HashSet<>(set1);
        union.addAll(set2);

        // 计算Jaccard相似度
        double jaccardIndex = intersection.size() / (double) union.size();
        return jaccardIndex;
    }

    // 将字符串转化为词频向量
    public static Map<String, Integer> getWordFrequency(String str) {
        Map<String, Integer> freqMap = new HashMap<>();
        for (int i = 0; i < str.length(); i++) {
            String charStr = String.valueOf(str.charAt(i));  // 单字处理，可以改成词语处理
            freqMap.put(charStr, freqMap.getOrDefault(charStr, 0) + 1);
        }
        return freqMap;
    }

    // 计算两个字符串的余弦相似度
    public static boolean cosineSimilarity(String str1, String str2) {
        Map<String, Integer> freqMap1 = getWordFrequency(str1);
        Map<String, Integer> freqMap2 = getWordFrequency(str2);

        // 计算两个词频向量的点积
        Set<String> words = new HashSet<>(freqMap1.keySet());
        words.addAll(freqMap2.keySet());

        double dotProduct = 0.0;
        double norm1 = 0.0;
        double norm2 = 0.0;

        for (String word : words) {
            int freq1 = freqMap1.getOrDefault(word, 0);
            int freq2 = freqMap2.getOrDefault(word, 0);

            dotProduct += freq1 * freq2;
            norm1 += Math.pow(freq1, 2);
            norm2 += Math.pow(freq2, 2);
        }

        if((dotProduct / (Math.sqrt(norm1) * Math.sqrt(norm2))) > 0.5){
            System.out.println("-----计算相似度----unit1---=: " + str1 + "---unit2---=" + str2);
            System.out.println("相似度: " + (dotProduct / (Math.sqrt(norm1) * Math.sqrt(norm2))));
        }

        // 计算余弦相似度
        return (dotProduct / (Math.sqrt(norm1) * Math.sqrt(norm2))) > 0.5;
    }


    public static void main(String[] args) {
//        String dateString = "201803";
//        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMM");
//
//        try {
//            // 使用 YearMonth 解析年月字符串
//            YearMonth yearMonth = YearMonth.parse(dateString, formatter);
//            System.out.println("Parsed YearMonth: " + yearMonth);
//            LocalDate firstDayOfMonth = yearMonth.atDay(1);
//            System.out.println("First day of the month: " + firstDayOfMonth);
//        } catch (DateTimeParseException e) {
//            e.printStackTrace();
//        }
//
//        String unit1 = "冀北经研院";
//        String unit2 = "国网冀北电力有限公司经济技术研究院";
//        String unit3 = "国网冀北电力有限公司";
//        String unit4 = "北京邮电大学";
//        String unit5 = "北京邮电大学心理咨询中心";
////        calculateSimilarity(unit1, unit2);
        double similarity1 = jaccardSimilarity("姚建国中国电科院", "姚建国国家电网公司科技部");
        System.out.println("两个单位的Jaccard相似度: " + similarity1);
//
        boolean similarity = cosineSimilarity("姚建国中国电科院", "姚建国国家电网公司科技部");
        System.out.println("余弦相似度: " + similarity);

    }

}
